import { Global, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigurationService } from './services/configuration.service';
import * as joi from 'joi';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { UserService } from './services/user.service';
import { IsUsernameUniqueConstraint } from './validators/user-validator';
@Global()
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      validationSchema:joi.object({
        MYSQL_HOST: joi.string().required(),
        MYSQL_PORT: joi.number().required(),
        MYSQL_USER: joi.string().required(),
        MYSQL_PASSWORD: joi.string().required(),
        MYSQL_DATABASE: joi.string().required(),
      })
    }),
    TypeOrmModule.forRootAsync({
      inject: [ConfigurationService],
      useFactory: (configService: ConfigurationService) => ({
        type: 'mysql',
        ...configService.mysqlConfig,
        autoLoadEntities: true,
        synchronize: true,
        logging: true,
      })
    }),

    TypeOrmModule.forFeature([
      User
    ])
  ],
  controllers: [],
  providers: [
    ConfigurationService,
    UserService,
IsUsernameUniqueConstraint
  ],
  exports: [ConfigurationService,UserService,IsUsernameUniqueConstraint],
})
export class SharedModule {}
